package com.jike.shop.cloud.mq.producer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.jike.shop.cloud.IPayService;
import com.jike.shop.cloud.entity.OrderInfo;
import com.jike.shop.cloud.entity.TransactionLog;
import com.jike.shop.cloud.enums.OrderStatusChangeEventEnum;
import com.jike.shop.cloud.enums.OrderStatusEnum;
import com.jike.shop.cloud.mapper.OrderInfoMapper;
import com.jike.shop.cloud.mapper.TransactionLogMapper;
import com.jike.shop.cloud.service.OrderInfoService;
import com.jike.shop.cloud.service.strategy.PayContext;
import com.jike.shop.cloud.service.strategy.PayStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.persist.StateMachinePersister;

import java.nio.charset.StandardCharsets;

@RocketMQTransactionListener(rocketMQTemplateBeanName = "rocketMQTemplate")
@Slf4j
public class MyTransactionImpl implements RocketMQLocalTransactionListener {

    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private TransactionLogMapper transactionLogMapper;



    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message mqMessage, Object o) {
        log.info("start invoke local rocketMQ transaction");
        RocketMQLocalTransactionState resultState = RocketMQLocalTransactionState.UNKNOWN;
        try {
            //处理业务
            String jsonStr = new String((byte[]) mqMessage.getPayload(), StandardCharsets.UTF_8);
            log.info("invoke msg content：{}", jsonStr);
            OrderInfo orderInfo = new Gson().fromJson(jsonStr, OrderInfo.class);
            // 获取事务消息ID
            String transId = (String) mqMessage.getHeaders().get(RocketMQHeaders.PREFIX + RocketMQHeaders.TRANSACTION_ID);
            PayContext.pay(orderInfo.getPaymentMethod(), orderInfo, transId);
            //log.info("本地事务已提交。{}",mqMessage.getHeaders().);
        } catch (Exception e) {
            log.error("invoke local mq trans error", e);
            resultState = RocketMQLocalTransactionState.ROLLBACK;
        }
        return resultState;
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        String transactionId = (String) message.getHeaders().get(RocketMQHeaders.PREFIX + RocketMQHeaders.TRANSACTION_ID);
        log.info("开始回查本地事务状态。{}", transactionId);
        RocketMQLocalTransactionState state;
        QueryWrapper<TransactionLog> transactionLogQueryWrapper = new QueryWrapper<>();
        transactionLogQueryWrapper.eq("transaction_id", transactionId);
        if (transactionLogMapper.selectCount(transactionLogQueryWrapper)>0){
            state = RocketMQLocalTransactionState.COMMIT;
        }else {
            state = RocketMQLocalTransactionState.UNKNOWN;
        }
        log.info("结束本地事务状态查询：{}",state);
        return state;
    }




}
